package com.example.day2.busi.goods;

import androidx.lifecycle.MutableLiveData;

import com.example.day2.entity.GoodsResult;
import com.example.day2.entity.GoodsTableResult;
import com.example.day2.mvvm.base.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class GoodsRepo extends BaseRepo {
    public void getGoodsList(int type, int page, int size, MutableLiveData<GoodsResult> success,MutableLiveData<String> fail){
        apiService.getGoods(type,page,size)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GoodsResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GoodsResult goodsResult) {
                        success.setValue(goodsResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        fail.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getGoodsTable(MutableLiveData<GoodsTableResult> success, MutableLiveData<String> fail){
        apiService.getGoodsTable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GoodsTableResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GoodsTableResult goodsTableRresult) {
                        success.setValue(goodsTableRresult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        fail.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
